import { handlerGobackClick, hint } from "../../../utils/util";
import { Pets } from "../../../models/pets";

const pets = new Pets();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    details: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.initPageData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  handlerGobackClick() {
    wx.redirectTo({
      url: "/owner-app/pages/pet/index",
    });
  },
  initPageData() {
    pets
      .getById(this.data.id)
      .then((res) => {
        this.setData({
          details: res.result,
        });
      })
      .catch((err) => {
        console.log(err);
      });
  },
  del() {
    pets
      .del(this.data.details.id)
      .then((res) => {
        hint(res.message);
        setTimeout(() => {
          wx.redirectTo({
            url: "/owner-app/pages/pet/index",
          });
        }, 1000);
      })
      .catch((err) => {
        console.log(err);
      });
  },
  editPet() {
    wx.navigateTo({
      url: "/owner-app/pages/add-pet/index?id=" + this.data.details.id,
    });
  },
});
